package com.dbq.fast.business.main.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dbq.fast.R;
import com.dbq.fast.business.product.ProductTypeActivity;

/**
 * Created by yuanchen on 2018/4/8.
 */

public class FreeDaysFragment extends DialogFragment {

    private String title = "";

    public static FreeDaysFragment newInstance(String title){
        FreeDaysFragment fragment = new FreeDaysFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static void showFreeDaysDialog(FragmentManager fragmentManager, String title){
        FragmentTransaction ft = fragmentManager.beginTransaction();
        FreeDaysFragment prev = (FreeDaysFragment) fragmentManager.findFragmentByTag("FREE_DAYS");
        if (prev != null) {
            if (prev.isVisible()) {
                prev.dismiss();
            }
            ft.remove(prev);
        }
        ft.addToBackStack(null);
        FreeDaysFragment adDialogFragment = FreeDaysFragment.newInstance(title);
        try {
            adDialogFragment.show(ft, "FREE_DAYS");
        }catch (Exception e){}
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = bundle.getString("title");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(false);// 点击外部不消失，点击X和返回键消失
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x4c000000));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            getDialog().getWindow().setDimAmount(0.45f);
        }
        getDialog().getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.free_days_dialog, null);

        TextView tvTitle = view.findViewById(R.id.free_days_desc);
        tvTitle.setText(Html.fromHtml(title));

        RelativeLayout goBuyArea = view.findViewById(R.id.area_go_buy);
        goBuyArea.setOnClickListener(view12 -> {
            Intent intent = new Intent(getActivity(), ProductTypeActivity.class);
            startActivity(intent);
            dismiss();
        });

        ImageView close = view.findViewById(R.id.free_days_close);
        close.setOnClickListener(view1 -> dismiss());
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
